package com.ruoyi.kong.entity;


import lombok.Data;
import org.springframework.util.ObjectUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Data
public class KongPlugin  implements Comparable {
    private Long created_at;
    private Map config;
    private String id;
    private EntityId service;
    private String name;
    private List<String> protocols;
    private boolean enabled;
    private String run_on;
    private EntityId consumer;
    private EntityId route;
    private String tags;
    // 自定义 插件应用范围 routes   services    global
    private String scope ;
    // 插件绑定实体
    private Map applyObject;

    @Override
    public int compareTo(Object o) {
        if (o == null) {
            return 1;
        }
        KongPlugin upstream = null;
        if (o instanceof KongPlugin) {
            upstream = (KongPlugin) o;
        }
        return upstream.created_at.compareTo(this.created_at);
    }

    public String getScope() {
        return scope;
    }

    public void setApplyTo(KongService services ,KongRoute routes, KongConsumer consumer) {
        Map map = new HashMap();
        if (route != null) {
            map.put("name", routes.getName());
            this.applyObject = map;
            this.scope = "routes";
            return;
        }
        if (service != null) {
            map.put("name", services.getName());
            this.applyObject = map;
            this.scope = "services";
            return;
        }

        if(!ObjectUtils.isEmpty(consumer)){
            map.put("name", consumer.getUsername());
            this.applyObject = map;
            this.scope = "consumer";
            return;
        }

        map.put("name", "All Entrypoints");
        scope="global";
        this.applyObject = map;
    }
}
